<?php

namespace tian;

class Url
{
    /**
     * URL生成
     * @param string $str
     * @param array $ary
     * @return string
     */
    public static function create(string $str = '', array $ary = []): string
    {
        if(config('url_common_param')){
            //传统模式
            $val_splicer = '=';
            $val_suffix = '&';

            $url_splicer = '.' . config('url_html_suffix') . '?';
            $url_suffix = '';
        }else{
            //优化模式
            $val_splicer = '/';
            $val_suffix = '/';

            $url_splicer = '/';
            $url_suffix = '.' . config('url_html_suffix');
        }

        if ($str != '') {
            //生成指定url
            if(0 !== strpos($str, '/')){
                //相对路径(需补全)
                $strAry = explode('/',$str);
                $strAryNum = count($strAry);

                if($strAryNum==1){
                    $url = request()->module() . config('pathinfo_depr') . request()->controller() . config('pathinfo_depr') . $str;
                }elseif($strAryNum==2){
                    $url = request()->module() . config('pathinfo_depr') . $str;
                }else{
                    $url = $str;
                }
                $url = '/' . $url;
            }else{
                //指定路径
                $url = $str;
            }
        } else {
            //生成当前页面url
            $url = request()->baseUrl();
            $action = request()->action();
            $urlAry = explode('/'.$action,$url);
            $url = $urlAry[0] . '/' . $action;
        }

        $vals = '';
        if (!empty($ary)) {
            $routeStatus = false;   //url 是否匹配到路由规则 默认 false
            $routeRule = [];        //匹配到的规则数组

            //优化模式下 且 指定路径模式 url生成自动遵循路由设定，如未匹配到路由规则，则参数串按队生成
            if(config('url_common_param')==false && 0 === strpos($str, '/')){
                $nameAll = Route::getRouteName();
                if(!empty($nameAll)){
                    foreach ($nameAll as $key => $item) {
                        if (0 === strpos($key, ltrim($str . '/:', '/'))) {
                            //匹配到路由规则
                            $routeStatus = true;
                            $routeRule = array_keys($item[0][1]);
                            break;
                        }
                    }
                }
            }

            if($routeStatus){
                //按照路由规则生成url
                foreach ($routeRule as $val) {
                    if(!empty($ary[$val])){
                        $vals .= urlencode(mb_convert_encoding($ary[$val], 'utf-8', 'auto')) . $val_suffix;
                    }
                }
            }else{
                //参数串按队生成
                foreach ($ary as $k => $val) {
                    if ($val != null) {
                        $vals .= $k . $val_splicer . urlencode(mb_convert_encoding($val, 'utf-8', 'auto')) . $val_suffix;
                    }
                }
            }

            $vals = rtrim($vals, '&');
            $vals = rtrim($vals, '/');
            $url = $url . $url_splicer . $vals . $url_suffix;
        }else{
            $url = $url . '.' . config('url_html_suffix');
        }

        return $url;
    }

}